package com.smart.member.controller;

import com.smart.common.vo.ResponseResult;
import com.smart.member.common.reqeust.MemberRequestParams;
import com.smart.member.entity.Member;
import com.smart.member.service.AuthService;
import com.smart.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 短信验证码注册
 * 用户输入手机号
 * 获取验证码
 * 生成验证码
 * 存入redis
 * mq发送消息给短信服务
 * <p>
 * 注册
 */
@CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
@RestController
@RequestMapping("/member")
@Api("会员模块")
public class MemberController {
    @Resource
    MemberService memberService;

    /**
     * @param phone
     * @return
     */
    @ApiOperation("获取手机验证码")
    @GetMapping("/code")
    public ResponseResult<String> code(@ApiParam("手机号") String phone) {
        return memberService.code(phone);
    }

    /**
     * @return
     */
    @ApiOperation("会员注册功能")
    @PostMapping("/register")
    public ResponseResult<Integer> register(@RequestBody MemberRequestParams member) {
        int msg = memberService.register(member);
        return ResponseResult.success(msg);
    }

    /**
     * @return
     */
    @ApiOperation("通过用户名获取会员信息")
    @PostMapping("/info")
    public ResponseResult<Member> getUserInfo(@ApiParam("用户名邮箱或者手机号") String username) {
        Member member = memberService.getMemberInfo(username);
        return ResponseResult.success(member);
    }

    /**
     * Security
     * jwt
     * 会员服务
     * 登录方法  用户名  密码
     * 远程调用 认证服务  http://127.0.0.1:9050/login
     * 认证服务
     * 服务 loadUserByUsername()---> 会员服务获取用户数据 (getUserInfo)
     * http://xxx:9060/user/info
     *
     * @param username
     * @param password
     * @return
     */

    /**
     * 2  后台认证
     * 1  移动端认证  pc端  小程序
     */
    @Resource
    AuthService authService;

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public ResponseResult<String> login(String username, String password) {
        return authService.login(username, password);
    }

}
